/**
 * 
 */
package com.me.CoaB;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;

/**
 * @author Space
 *
 */
public class MainCharacter {
	
	// Dude
	
	private static final int FRAME_COLS = 6;
	private static final int FRAME_ROWS = 5;
	
	Animation		guyAnim;
	Texture			guySheet;
	TextureRegion[]	guyFrames;
	TextureRegion	currentFrame;
	
	private static final float RUNNING_FRAME_DURATION = 0.025f;
	
	float stateTime = 0;
	
	public enum State {
		IDLE,WALKING,
		jumping,dying,
		shooting,
	}
	
	public enum Dir {
		LEFT, RIGHT, UP, DOWN
	}
	
	public final float SPEEDMAX = 2f;
	public final float SPEED = 4f;
	public final float SIZE = .5f;
	// jumping velocity
	
	public Vector2 position = new Vector2();
	public Vector2 acceleration = new Vector2();
	public Vector2 velocity = new Vector2();
	Rectangle bounds = new Rectangle();
		
	State state = State.WALKING;
	Dir dir = Dir.LEFT;
	public MainCharacter(Vector2 position) {
		this.position = position;
		this.bounds.height = SIZE;
		this.bounds.width = SIZE;
		this.getVelocity().x = SPEED;
		// guy animation --> NOT SURE WHERE TO THROW THIS??>
		guySheet = new Texture(Gdx.files.internal("animation_sheet.png"));
		TextureRegion[][] tmp = TextureRegion.split(guySheet, guySheet.getWidth() / FRAME_COLS, guySheet.getHeight() / FRAME_ROWS);
		
		guyFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
		int index = 0;
		for ( int i = 0; i < FRAME_ROWS; i++) {
			for (int j = 0; j < FRAME_COLS; j++) {
				// System.out.print(index);
				guyFrames[index++] = tmp[i][j];
			}		
		}
		
		
	}
	
	public void UpdatePlayer(float deltatime){
		if(deltatime==0)return;
		
		//stateTime += deltatime;
		//System.out.print("deltat:"+deltatime+"\n");
		position.add(velocity.tmp().mul(deltatime));
		
		guyAnim = new Animation(RUNNING_FRAME_DURATION,guyFrames);
		currentFrame = guyAnim.getKeyFrame(deltatime, true);	
	}
	
	public void setState(State newState) {
		this.state = newState;
	}
	public Vector2 getPosition() {
		return position;
	}

	public Vector2 getAcceleration() {
		return acceleration;
	}

	public Vector2 getVelocity() {
		return velocity;
	}

	public Rectangle getBounds() {
		return bounds;
	}

	public State getState() {
		return state;
	}
}
